import Vue from "vue";
import VueRouter from "vue-router";
import { clearLoginInfo } from '../utils/index'

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        beforeEnter (to, from, next) {
            let token = Vue.cookie.get('token')
            if (!token || !/\S/.test(token)) {
                console.log("11")
                clearLoginInfo()
                // next()
                next({path: '/login'})
            } else {
                // console.log(token)
               next({path: '/home/bar'})
            }
            // next()
        }
    },

    {
        path: '/home',
        component: () => import('../view/home'),
        children: [{
            path: 'bar',
            component: () => import('../components/side/side-navigation')
        }, {
            path: '/show-environment',
            component: () => import('../components/charts/environment-charts'),
        }, {
            path: '/show-nutrition',
            component: () => import('../components/charts/nutrition-charts'),
        }, {
            path: '/show-yield',
            component: () => import('../components/charts/yield-charts'),
        }, {
            path: '/show-lot',
            component: () => import('../components/charts/lot-charts'),
        }, {
            path: '/show-task',
            component: () => import('../components/task/task-select'),
        }, {
            path: '/show-collect-task',
            component: () => import('../components/task/task-all'),
        },],
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../view/login')
    }

    // {
    //     path: '/backstage',
    //     component:() => import('../view/backstage/home'),
    //     children: [
    //         {path : '/file-upload' , component: ()=> import('../components/file/file-upload')},
    //         {path : '/file-down' , component: ()=> import('../components/file/file-down')},
    //         {path : "/show-environment" , component: ()=> import('../components/charts/environment-data')},
    //         {path : '/show-product' , component:()=> import('../components/charts/product-target')},
    //         {path : '/show-nutrition' , component:()=> import('../components/charts/nutrition-state')},
    //         {path : '/show-LOT' , component:()=> import('../components/charts/lot-data')},
    //         {path : '/show-map' , component:()=> import('../components/map/map-show-location-task')},
    //         {path : '/show-photo' , component:()=> import('../components/map/soil-photo')},
    //         {path : '/show-task' , component:()=> import('../components/task/task-info')}
    //     ]
    // }
    // {path: '/', component:()=> import('../components/person/login')}
]

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}
const router = new VueRouter({
    routes,
})


export default router